/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package psw;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JPanel;

/**
 *
 * @author tabakkie.
 */
public class AnswerBox extends JPanel {

    private RoundRectangle2D.Double checkBox;
    private String answer;
    private int leftPanelHeight;
    private int leftPanelWidth;
    private int spacer;
    private int boxHeight;
    private int boxWidth;
    private int boxCorner;
    private int number; // het nummer van deze checkbox
    private Graphics2D g2;
    private GUI gui;
    private boolean isChecked = false;

    public AnswerBox(String answer, int leftPanelHeight, int leftPanelWidth, int number, GUI gui) {
        this.number = number;
        this.gui = gui;
        spacer = leftPanelHeight / 130;
        boxHeight = (leftPanelHeight - (12 * spacer)) / 11;
        boxWidth = (leftPanelWidth - (2 * spacer));
        boxCorner = boxHeight / 4;
        checkBox = new RoundRectangle2D.Double(spacer, spacer, boxHeight - (2 * spacer), boxHeight - (2 * spacer), boxCorner, boxCorner);
        if (answer != null && !answer.trim().equals("")) {
            this.answer = answer;
        }
        this.leftPanelHeight = leftPanelHeight;
        this.leftPanelWidth = leftPanelWidth;
        addMouseListener(new MyMouseListener());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke(2));
        if(isChecked) {
            GradientPaint gp = new GradientPaint(0, 0, Color.YELLOW, (float)checkBox.getWidth(), (float)checkBox.getHeight(), Color.blue);
            g2.setPaint(gp);
            g2.fill(new RoundRectangle2D.Double(checkBox.getX() + 8, checkBox.getY() + 8, checkBox.getWidth() - 15, checkBox.getHeight() - 15, boxCorner, boxCorner));
        }
        g2.setColor(Color.black);
        g2.draw(checkBox);
        
        // ---------------------   text   ----------------------------------                 
        Font font = new Font("Arial Black", Font.PLAIN, 25);
        FontMetrics fm   = g2.getFontMetrics(font);
        Rectangle2D rect = fm.getStringBounds(answer, g2);       

        int xString = (int) (checkBox.getX() + checkBox.getWidth() + spacer);
        int yString = (int) (checkBox.getY() + (checkBox.getHeight() / 2 ) + (rect.getHeight() / 4));
        g2.setFont(font);        
        g2.drawString(answer, xString, yString);
        
    }
    
    class MyMouseListener extends MouseAdapter
    {

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            if (isChecked) {
                return;
            }
            isChecked = true;
            repaint();
        }
        
    }
    
}
